package com.bdqn.boot.yuanshen.controller;

import com.bdqn.boot.yuanshen.bean.Users;
import com.bdqn.boot.yuanshen.service.FollowsService;
import com.bdqn.boot.yuanshen.service.UsersService;
import com.bdqn.boot.yuanshen.util.JwtUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fly
 * @version 1.0
 * @className UsersController
 * @description TODO
 * @date 2025/3/12 09:53
 */
@Slf4j
@RestController
@CrossOrigin
public class UsersController {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private UsersService usersService;
    @Autowired
    private FollowsService followsService;

    @GetMapping("/Users")
    public PageInfo<Users> getUsers(@RequestParam(defaultValue = "1") int pageNum,
                                    @RequestParam(defaultValue = "10") int pageSize) {
        return usersService.getUsers(pageNum, pageSize);
    }

    @GetMapping("/Users/page")
    public PageInfo<Users> getUsersByUserName(@RequestParam String username,
                                    @RequestParam(defaultValue = "1") int pageNum,
                                    @RequestParam(defaultValue = "10") int pageSize) {
        return usersService.getUsers(username ,pageNum, pageSize);
    }


    @PostMapping("/Users")
    public String saveUsers(@RequestBody Users users) {
        if (users.getId()==null || users.getId()==0) {
            usersService.addUsers(users);
            return "success";
        } else {
            usersService.updateUser(users);
            return "update";
        }
    }


    @DeleteMapping("/Users/{id}")
    public String deleteUsers(@PathVariable int id) {
        usersService.deleteUser(id);
        return "delete";
    }

    @GetMapping("/Users/{id}")
    public Users getUsers(@PathVariable int id) {
        return usersService.getUser(id);
    }

    @PostMapping("/logins")
    public Map<String, Object> login(@RequestBody Users users) {
        Map<String, Object> result = new HashMap<>();
        Users user = usersService.login(users.getUsername(), users.getPassword());

        if(user == null) {
            result.put("success", false);
            result.put("msg", "用户名或密码错误");
            return result;
        }

        if(user.getStatus() == 1) {
            result.put("success", false);
            result.put("msg", "账号已被封禁");
            return result;
        }

        // 生成Token
        String token = jwtUtil.generateToken(user);
        result.put("success", true);
        result.put("token", token);
        result.put("user", user);
        return result;
    }
    @PostMapping("/userinfo")
    public Map<String, Object> getUserInfo(@RequestBody Users user) {
        Map<String, Object> result = new HashMap<>();
        try {
            Users dbUser = usersService.getUserById(user.getId());
            Map<String, Object> userInfo = followsService.getFollowerAndFollowedCount(user.getId());

            // 创建一个 userMap，将 dbUser 和 userInfo 存放到其中
            Map<String, Object> userMap = new HashMap<>();
            userMap.put("user", dbUser);
            userMap.put("userInfos", userInfo);

            // 将 userMap 放入 result 中
            result.put("success", true);
            result.put("userMap", userMap);
        } catch (Exception e) {
            result.put("success", false);
        }
        return result;
    }



    @PostMapping("/userinfo/update")
    public Map<String, Object> updateUserInfo(@RequestBody Users user) {
        Map<String, Object> result = new HashMap<>();
        try {
            usersService.updateUser(user);
            result.put("success", true);
            result.put("msg", "用户信息更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", "用户信息更新失败");
        }
        return result;
    }
}